/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diagram.handles;

import diagram.Node;
import diagram.locations.AbsoluteLocation;
import diagram.locations.FlexibleRelativeLocation;
import diagram.locations.Location;
import diagram.locations.VerticalLineLocation;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;

/**
 *
 * @author Jelle
 */
public class TriangleHandle extends Handle {

    protected int size;	// size of the square
    private // size of the square
    Location p1;
    private Location p2;
    private Location p3;
    private Location pp1, pp2, pp3;

    public TriangleHandle() {
    }

    public TriangleHandle(Node baseNode, Location location, int size) {
        super(baseNode, new VerticalLineLocation(new FlexibleRelativeLocation(location,0,0), location, new AbsoluteLocation(location.getX(), location.getY()+50)));

       // new VerticalLineLocation(new FlexibleRelativeLocation(baseNode.getUpperLeft(), 0, 0),baseNode.getUpperLeft(), baseNode.getLowerRight()), minnSize);


        
        this.size = size;

    }

    public boolean contains(int x, int y) {
        boolean ok = true;
        if (x < getP1().getX() || x > getP3().getX()) {
            ok = false;
        }
        if (y < getP1().getY() || y > getP2().getY()) {
            ok = false;
        }
        return ok;
    }

    public void setLocation(Location location) {
        super.setLocation(location);
    }

    public void setSize(int size) {
        this.size = size;
    }

    public void paint(Graphics2D g) {
        p1 = new FlexibleRelativeLocation(new AbsoluteLocation(((Node)getBase()).getLowerRight().getX(),((Node)getBase()).getUpperLeft().getY()),0,0);
        p2 = new FlexibleRelativeLocation(new AbsoluteLocation(((Node)getBase()).getLowerRight().getX(),((Node)getBase()).getUpperLeft().getY()+30),0,0);
        p3 = new FlexibleRelativeLocation(new AbsoluteLocation(((Node)getBase()).getLowerRight().getX()+20,((Node)getBase()).getUpperLeft().getY()+15),0,0);
        Color oldColor = g.getColor();


        if (((Node)getBase()).isSelected()) {
            g.setColor(Color.RED);
        } else {
            g.setColor(Color.BLACK);
        }
        int[] xs = {getP1().getX(), getP2().getX(), getP3().getX()};
        int[] ys = {getP1().getY(), getP2().getY(), getP3().getY()};
        Polygon triangle = new Polygon(xs, ys, xs.length);
        g.drawPolygon(triangle);
     


        
        // g.draw(this.getTriangle());
        if (this.isHighlighted()) {
            //g.setColor(Color.RED);
            getHighlightTriangle();
            int[] xsa = {pp1.getX(), pp2.getX(), pp3.getX()};
            int[] ysa = {pp1.getY(), pp2.getY(), pp3.getY()};
            triangle = new Polygon(xsa, ysa, xsa.length);
            g.drawPolygon(triangle);
        }

        g.setColor(oldColor);
    }

    protected void getHighlightTriangle() {
       pp1 = new FlexibleRelativeLocation(new AbsoluteLocation(p1.getX()-4, p1.getY()-8),0,0);
       pp2 = new FlexibleRelativeLocation(new AbsoluteLocation(p2.getX()-4, p2.getY()+8),0,0);
       pp3 = new FlexibleRelativeLocation(new AbsoluteLocation(p3.getX()+4, p3.getY()),0,0);

    }

    /**
     * @return the p1
     */
    public Location getP1() {
        return p1;
    }

    /**
     * @param p1 the p1 to set
     */
    public void setP1(Location p1) {
        this.p1 = p1;
    }

    /**
     * @return the p2
     */
    public Location getP2() {
        return p2;
    }

    /**
     * @param p2 the p2 to set
     */
    public void setP2(Location p2) {
        this.p2 = p2;
    }

    /**
     * @return the p3
     */
    public Location getP3() {
        return p3;
    }

    /**
     * @param p3 the p3 to set
     */
    public void setP3(Location p3) {
        this.p3 = p3;
    }
}
